#include "fxcc/core/graph/opengl3/ImGuiGlfwApp.h"
#include "fxcc/core/graph/common/Geometry.h"
#include "fxcc/core/graph/opengl3/TextureCubeJack.h"

#include "fxcc/imgui/Editor.h"
#include "fxcc/core/AssertManager.h"

using namespace fxcc::platform;
using namespace fxcc::graph;
using namespace fxcc::graph::opengl3;
using namespace fxcc::imgui::graph::opengl3;
using BaseApp = fxcc::imgui::graph::opengl3::GlfwApp;

#undef main

struct SimpleApp : public BaseApp
{
public:
    std::shared_ptr<fxcc::graph::opengl3::RenderTargetJack> m_RenderTarget;
    std::shared_ptr<fxcc::graph::opengl3::Texture2DJack> m_Texture;


    fxcc::graph::common::Camera camera;

    SimpleApp(const fxcc::platform::common::App::Desc& desc) : BaseApp(desc)
    {
    }
    bool Init() override
    {
        if (!BaseApp::Init())
        {
            return false;
        }

        if (!InitResources())
        {
            return false;
        }

        return true;
    };

    void printOpenGLESVersion()
    {
        const char* version = (const char*)glGetString(GL_VERSION);
        std::cout << "OpenGL ES Version: " << version << std::endl;
    };

    bool InitResources()
    {
        auto& am = fxcc::AssertManager::Inst();
        std::string imagePath = fxcc::AssertManager::GetAbsoluteFile("Texture/daylight0.png");
        std::string ddsPath  = fxcc::AssertManager::GetAbsoluteFile("Texture/d1.dds");
        fxcc::graph::common::Texture2DJack::Desc desc;
        desc.SetDDS(ddsPath);
        auto texture = std::make_shared<fxcc::graph::opengl3::Texture2DJack>(desc);
        texture->CreateDDSMipmap("out4.dds");

        return true;
    };

    fxcc::imgui::Editor editor;

    void OnEdit() override
    {
        ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());

        ImGui::Begin("Hello world");
        editor.AddFrameBuffer(m_RenderTarget->m_Textures[0]->GetTextureId());
        ImGui::End();

        ImGui::Begin("Texture Cube");

        ImGui::End();

        
    }

    void OnDestroy() override
    {
        BaseApp::OnDestroy();
    }

    void OnFrameRender() override
    {
        glViewport(0, 0, m_Desc.m_Size.x, m_Desc.m_Size.y);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


        //for (int i = 0; i < 6; i++)
        //{
        //    auto rt = m_FaceRT[i];
        //    rt->Begin();
        //    glClearColor(color.x, color.y, color.z, color.w);
        //    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        //    m_CubeFaceEffect->Begin();
        //    //m_CubeFaceEffect->SetCamera(camera);
        //    Effect3d::PassData passData;
        //    passData.m_Proj = fxcc::graph::common::TextureCubeJack::captureProjection;
        //    passData.m_View = fxcc::graph::common::TextureCubeJack::captureViews[i];
        //    passData.m_ProjView = passData.m_Proj * passData.m_View;

        //    m_CubeFaceEffect->m_Shader->Use();
        //    m_CubeFaceEffect->m_Shader->setInt("cubeMap", 0);
        //    m_CubeFaceEffect->m_Shader->setInt("gamma", m_TextureCubeJack->m_Desc.m_HDRConfig.m_HDR);
        //    m_TextureCubeJack->Active(0);

        //    m_CubeFaceEffect->SetPassData(passData);
        //    m_CubeFaceEffect->SetObjData(transform);
        //    m_CubeFaceEffect->InstMeshJack(m_CubeMesh.get());
        //    m_CubeFaceEffect->End();

        //    rt->End();
        //}

    };

    void OnFramebuffersize(int w, int h) override
    {
        opengl3::GlfwApp::OnFramebuffersize(w, h);
        camera.SetAspect(m_Desc.Aspect());

        m_RenderTarget = std::make_shared<fxcc::graph::opengl3::RenderTargetJack>(fxcc::graph::common::RenderTargetJack::Desc(1, { w,h, 4, 1 }));
    }
    void OnUpdate() override
    {
        opengl3::GlfwApp::OnUpdate();

        float ctTime = m_GlfwlTimer.m_CurrentTime;
        camera.FreeMove(m_Input, m_GlfwlTimer.m_DeltaTime);

        float deltaTime = m_GlfwlTimer.m_DeltaTime;
    }
};

int main()
{
    ztclog::pushFunction([](const ztclog::Line& line)
        {
            {
                std::cout << line.m_Text << std::endl;
            } });

            using _App = SimpleApp;
            using _Desc = fxcc::platform::common::App::Desc;

            _Desc desc;
            desc.m_Pos = glm::ivec2(100, 100);
            desc.m_Size = glm::ivec2(900, 600);
            desc.m_ScaleFactor = 1.0f;

            desc.m_Title = "test font";
            std::shared_ptr<_App> _app = std::make_shared<_App>(desc);
            return _app->Run();
};
